package com.videomeetting.legend.videomeetting.activity.fragment;

import android.Manifest;
import android.content.ContentResolver;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.PluralsRes;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.videomeetting.legend.videomeetting.R;
import com.videomeetting.legend.videomeetting.activity.adapater.FileChooseAdapater;
import com.videomeetting.legend.videomeetting.activity.base.BaseApplication;
import com.videomeetting.legend.videomeetting.activity.bean.MessageEvent;
import com.videomeetting.legend.videomeetting.activity.bean.VideoBean;
import com.videomeetting.legend.videomeetting.activity.utils.MyUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by legend on 18-6-29.
 */

public class ChannelsFragment extends Fragment {
    private List<VideoBean> extralVideos;
    private EditText filechooseEdit;
    private EditText encodeUrlEt;
    private SharedPreferences sharedPreferences;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new Thread(new Runnable() {
            @Override
            public void run() {
                if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED){
                    ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 123);
                }else {
                    //内部储存
                    extralVideos = MyUtils.queryLocalVideo(getActivity(), MediaStore.Video.Media.EXTERNAL_CONTENT_URI,0);//getStorageDirectory
                }
            }
        }).start();
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode){
            case 123:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    extralVideos = MyUtils.queryLocalVideo(getActivity(), MediaStore.Video.Media.EXTERNAL_CONTENT_URI,0);
                }else {
                    Toast.makeText(getActivity(), "You denied the permission", Toast.LENGTH_SHORT).show();
                }
        }
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(MessageEvent messageEvent) {
        if (messageEvent.getCode()==5) {
            Toast.makeText(getActivity(), messageEvent.getMessage(), Toast.LENGTH_SHORT);
            filechooseEdit.setText(messageEvent.getMessage());
        }
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        EventBus.getDefault().register(this);
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_channels,container,false);
        sharedPreferences = getActivity().getSharedPreferences("channel", Context.MODE_PRIVATE);
        String encode = sharedPreferences.getString("encode", "");
        Button codeSettingBtn = (Button) view.findViewById(R.id.fragment_channels_code_setting_btn);
        Button saveBtn = (Button) view.findViewById(R.id.fragment_channels_save_btn);
        encodeUrlEt = (EditText) view.findViewById(R.id.fragment_channels_encode_url_et);
        if (!TextUtils.isEmpty(encode)) {
            encodeUrlEt.setText(encode);
        }
        filechooseEdit = (EditText) view.findViewById(R.id.fragment_channels_local_setting_files);
        Button localSettingBtn = (Button) view.findViewById(R.id.fragment_channels_local_setting_btn);
        final FileChooseAdapater adapater = new FileChooseAdapater(getActivity(),extralVideos);
        codeSettingBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextChooseFragment speedTestFragment = new TextChooseFragment();
                speedTestFragment.show(getChildFragmentManager(),"textchoose");
            }
        });
        saveBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String encodeUrl = encodeUrlEt.getText().toString().trim();
                SharedPreferences.Editor edit = sharedPreferences.edit();
                edit.putString("encode",encodeUrl);
                edit.commit();
            }
        });
        localSettingBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FileChooseFragment speedTestFragment = new FileChooseFragment();
                speedTestFragment.setAdapater(adapater);
                speedTestFragment.show(getChildFragmentManager(),"filechose");
            }
        });
        return view;
    }

}
